package com.sxsihe.heartmate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;

import java.util.Timer;
import java.util.TimerTask;

public class UserInfoInputActivity extends Activity {
    TextView title ;
    EditText editText;
    TextView save;
    RadioGroup sex_radio;
    ImageView back_icon;

    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.userinfo_input);
        editText = (EditText)findViewById(R.id.edittext);
        sex_radio = (RadioGroup)findViewById(R.id.sex_radio);

        save = (TextView)findViewById(R.id.save);
        final String type = getIntent().getStringExtra("type");
        title = (TextView)findViewById(R.id.title);
        if (type.equals("name")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写姓名");
        }else if(type.equals("age")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写年龄");
        }else if(type.equals("sex")) {
            toggleSex(true);
            toggleInput(false);
            if(getIntent().getStringExtra("value").equals("男")){
                sex_radio.check(R.id.male);
            }
            else if(getIntent().getStringExtra("value").equals("女")){
                sex_radio.check(R.id.female);
            }
            title.setText("填写性别");
        }else if(type.equals("ID")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写身份证号");
        }else if(type.equals("phone")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写手机号");
        }else if(type.equals("linkman")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写联系人姓名");
        }else if(type.equals("linkman_phone")) {
            toggleSex(false);
            toggleInput(true);
            title.setText("填写联系人手机号");
        }
        editText.setText(getIntent().getStringExtra("value"));

        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();

//打开软键盘
//        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = "";
                if (editText.getVisibility() == View.VISIBLE) {
                    value = editText.getText().toString();
                }
                if (sex_radio.getVisibility() == View.VISIBLE) {
                    value = ((RadioButton) findViewById(sex_radio.getCheckedRadioButtonId())).getText().toString();
                }
                Intent intent = new Intent();
                intent.putExtra("value", value);
                intent.putExtra("type", type);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.in_left, R.anim.out_right);

            }
        });

        back_icon = (ImageView)findViewById(R.id.back_icon);
        back_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.in_left, R.anim.out_right);
            }
        });

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(editText, 0);
                           }

                       },
                200);

    }

    private void toggleSex(boolean show){
        if(show){
            sex_radio.setVisibility(View.VISIBLE);
        }else{
            sex_radio.setVisibility(View.GONE);
        }
    }

    private void toggleInput(boolean show){
        if(show){
            editText.setVisibility(View.VISIBLE);
        }else{
            editText.setVisibility(View.GONE);
        }
    }

    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.in_left, R.anim.out_right);
    }
}
